<?php

namespace App\Http\Controllers\Innerend;


use App\Models\SiteCate;
use App\Models\TopicCate;
use App\Service\Tree;

class TopicController extends BaseController
{
    // 话题
    public function initialize()
    {
        parent::initialize();

        $this->model = 'topic';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return ['list' => $lists, 'tr' => $trList];
        }

        // 栏目
        $cateDb = new TopicCate();
        $cids   = array_column($lists, 'cate_id');
        $cates  = $cateDb->isCache(true)->getListInIds($cids);

        // 展示列表表格
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id  = $item['id'];
            $_cid = $item['cate_id'];
            // 栏目
            $_cateName = $cates[$_cid]['title'] ?? '-';
            $status    = $item['status'] == 1 ? '正常' : '禁用';
            $statusOp  = $item['status'] == 1 ? '禁用' : '启用';
            if($item['image']){
                $image     = $this->showQiniuImage($item['image'], 100, 200);
                $imageHtml = "<img src='".$image."' style='width:100px;' >";
            }

            // 操作
            $operate = $listHtml->operate_status(['id' => $_id, 'status' => 1 - $item['status']], $statusOp, $item['status']);
            $operate .= $listHtml->operate_edit(['id' => $_id], '编辑');
            $operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该话题吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($_cateName);
            $tr .= $listHtml->list_td($item['title']);
            $tr .= $listHtml->list_td($imageHtml);
            $tr .= $listHtml->list_td($item['rank']);
            $tr .= $listHtml->list_td($status);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);
            $tr .= $operate;

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return ['list' => $lists, 'tr' => $trList];
    }

    // 列表字段
    protected function list_fields()
    {
        // 'role' => '角色',
        return ['cate_id' => '话题栏目', 'title' => '话题名称', 'image' => '图片', 'rank' => '排序', 'status' => '状态', 'ctime' => ['创建时间', 120], 'operate' => ['操作', 250],];
    }

    // 表单页面
    protected function formTable()
    {
        $html = '';
        $info = $this->onShow();
        $form = $this->adminForm();

        // 栏目
        $model = new TopicCate();
        $where = [['is_del', '=', 0],];
        $lists = $model->isCache(true)->getAll($where, 'rank ASC');
        $cates = $form->formSelectLists('cate_id', $info['cate_id'] ?? '0', $lists, 'id', 'title');


        $title  = $form->formInput('title', $info['title'] ?? '', '话题名称');
        $image  = $form->formImage('image', $info['image'] ?? '');
        $rank   = $form->formNumber('rank', $info['rank'] ?? '', '排序');
        $status = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        $_id    = $form->formHidden('id', $info['id'] ?? '');

        $html .= $form->makeItem('话题栏目', $cates);
        $html .= $form->makeItem('话题名称', $title);
        $html .= $form->makeItem('图片', $image);
        $html .= $form->makeItem('排序', $rank);
        $html .= $form->makeItem('status', $status);
        $html .= $_id;

        return $html;
    }

    // 列表搜索
    protected function list_search()
    {
        $cateDb = new TopicCate();
        $cates  = $cateDb->isCache(true)->getList(['is_del' => 0]);

        $request = $this->getData();
        $form    = $this->adminList();
        $keyword = $form->formInput('keyword', $request['keyword'] ?? '', '关键字');
        $cate_id = $form->formSelectLists('cate_id', $request['cate_id'] ?? '0', $cates, 'id', 'title');

        $search = ['keyword' => $keyword, 'cate_id' => $cate_id];  // keyword
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }


    // 列表查询
    protected function listWhere()
    {
        $where   = [['status', '>=', '0']];
        $cate_id = $this->getData('cate_id', null, 'intval');
        $keyword = $this->getData('keyword', null, 'trim');
        if($keyword){
            $where[] = $this->orWhere('title|describe|tag', 'like', $keyword);
        }
        if(!is_null($cate_id)){
            $where[] = ['cate_id', '=', $cate_id];
        }

        return $where;
    }

    // 获取关注话题的用户列表
    public function users()
    {
        $t_id = $this->getData('topic_id', 0, 'intval');
        $this->throwIf(empty($t_id), "请指定话题编号！！");

        // 参数
        $page  = $this->page;
        $rows  = $this->rows;
        $model = $this->checkModel('TopicSubscribe');
        if($model === false){
            return $this->error($this->error);
        }

        // 模型 和 查询
        $result = $model->getUsers(["topic_id" => $t_id], $page, $rows, "id DESC");

        return $this->success($result);
    }

    // 启用禁用
    public function status()
    {
        $_value = $this->getData('status', 0, 'intval');
        $this->throwIf(is_null($_value), '是否启用状态必须填写！');
        $this->throwIf(!in_array($_value, [0, 1]), '是否启用状态必须为0或1！');

        return $this->changeField('status', $_value);
    }

    // 是否推荐
    public function propose()
    {
        $_value = $this->getData('is_propose', 0, 'intval');
        $this->throwIf(is_null($_value), '是否启用状态必须填写！');
        $this->throwIf(!in_array($_value, [0, 1]), '是否启用状态必须为0或1！');

        return $this->changeField('is_propose', $_value);
    }

    // 查找
    public function show()
    {
        $this->throwIf(empty($this->id), '请指定查看记录');

        $where = ['id' => $this->id];
        $model = $this->checkModel($this->model);
        $info  = $model->isCache(true)->oneFind($where);
        $this->throwIf($info === false, $model->getError());

        // 数据整理
        $info['image'] = $this->getFullUrl($info['image']);
        if($info['cate_id']){
            $cateDb = new TopicCate();
            $cate   = $cateDb->isCache(true)->getOneById($info['cate_id']);
            if(empty($cate)){
                $cate = (object)[];
            }
            // 整理
            $info['cate'] = $cate;
        }

        return $this->success($info);
    }

    // 删除（软删除）
    public function del()
    {
        $this->throwIf(empty($this->id), '请指定删除记录');

        $where  = ['id' => $this->id];
        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->oneEdit($where, ['status' => -1]);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 添加
    protected function _before_add()
    {
        $data = [
            //'follow_no'         => $this->getData('follow_no', 0, 'intval'),
            //'share_no'          => $this->getData('share_no', 0, 'intval'),
            //'virtual_follow_no' => $this->getData('virtual_follow_no', 0, 'intval'),
            'title'      => $this->getData('title', null, 'trim'),
            'image'      => $this->getData('image', null, 'trim'),
            'describe'   => $this->getData('describe', null, 'trim'),
            'tag'        => $this->getData('tag', null, 'trim'),
            'cate_id'    => $this->getData('cate_id', 0, 'intval'),
            'is_propose' => $this->getData('is_propose', 0, 'intval'),
            'rank'       => $this->getData('rank', 0, 'intval'),
            'status'     => $this->getData('status', 1, 'intval'),
            'verify'     => $this->getData('verify', 1, 'intval'),
        ];

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            //'follow_no'         => $this->getData('follow_no', 0, 'intval'),
            //'share_no'          => $this->getData('share_no', 0, 'intval'),
            //'virtual_follow_no' => $this->getData('virtual_follow_no', 0, 'intval'),
            'title'      => $this->getData('title', null, 'trim'),
            'image'      => $this->getData('image', null, 'trim'),
            'describe'   => $this->getData('describe', null, 'trim'),
            'tag'        => $this->getData('tag', null, 'trim'),
            'cate_id'    => $this->getData('cate_id', 0, 'intval'),
            'is_propose' => $this->getData('is_propose', 0, 'intval'),
            'rank'       => $this->getData('rank', 0, 'intval'),
            'status'     => $this->getData('status', 1, 'intval'),
            'verify'     => $this->getData('verify', 1, 'intval'),
        ];

        return $data;
    }

}


